package org.dromara.bclt.service.impl;

import org.dromara.bclt.domain.DeviceInfo;
import org.dromara.bclt.domain.bo.DeviceInfoBo;
import org.dromara.bclt.domain.vo.DeviceInfoVo;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bclt.mapper.DeviceInfoMapper;
import org.dromara.bclt.service.IDeviceInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 手环-设备信息Service业务层处理
 *
 * @author RRyuan
 * @date 2024-10-21
 */
@RequiredArgsConstructor
@Service
public class DeviceInfoServiceImpl implements IDeviceInfoService {

    private final DeviceInfoMapper baseMapper;

    /**
     * 查询手环-设备信息
     *
     * @param id 主键
     * @return 手环-设备信息
     */
    @Override
    public DeviceInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询手环-设备信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 手环-设备信息分页列表
     */
    @Override
    public TableDataInfo<DeviceInfoVo> queryPageList(DeviceInfoBo bo, PageQuery pageQuery) {
        if (bo.getDeviceNumber() != null) {
            DeviceInfoVo deviceInfoVo = baseMapper.selectVoOne (new LambdaQueryWrapper<DeviceInfo>()
                .select(DeviceInfo::getId)
                .eq(DeviceInfo::getDeviceNumber, bo.getDeviceNumber()));
            Long id = null;
            if (deviceInfoVo != null) {
                id = deviceInfoVo.getId();
            }
            bo.setId(id);
        }
        LambdaQueryWrapper<DeviceInfo> lqw = buildQueryWrapper(bo);
        Page<DeviceInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的手环-设备信息列表
     *
     * @param bo 查询条件
     * @return 手环-设备信息列表
     */
    @Override
    public List<DeviceInfoVo> queryList(DeviceInfoBo bo) {
        LambdaQueryWrapper<DeviceInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DeviceInfo> buildQueryWrapper(DeviceInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DeviceInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceNumber()), DeviceInfo::getDeviceNumber, bo.getDeviceNumber());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), DeviceInfo::getUserName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), DeviceInfo::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), DeviceInfo::getSex, bo.getSex());
        lqw.eq(bo.getAge() != null, DeviceInfo::getAge, bo.getAge());
        lqw.eq(bo.getHeight() != null, DeviceInfo::getHeight, bo.getHeight());
        lqw.eq(bo.getWeight() != null, DeviceInfo::getWeight, bo.getWeight());
        lqw.like(StringUtils.isNotBlank(bo.getAddress()), DeviceInfo::getAddress, bo.getAddress());
        lqw.like(StringUtils.isNotBlank(bo.getPhone()), DeviceInfo::getPhone, bo.getPhone());
        lqw.eq(bo.getCreateTime() != null, DeviceInfo::getCreateTime, bo.getCreateTime());
        return lqw;
    }

    /**
     * 新增手环-设备信息
     *
     * @param bo 手环-设备信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(DeviceInfoBo bo) {
        DeviceInfo add = MapstructUtils.convert(bo, DeviceInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改手环-设备信息
     *
     * @param bo 手环-设备信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(DeviceInfoBo bo) {
        if (bo.getDeviceNumber() != null) {
            DeviceInfoVo deviceInfoVo = baseMapper.selectVoOne(new LambdaQueryWrapper<DeviceInfo>()
                .select(DeviceInfo::getId)
                .eq(DeviceInfo::getDeviceNumber, bo.getDeviceNumber()));
            Long id = null;
            if (deviceInfoVo != null) {
                id = deviceInfoVo.getId();
            }
            bo.setId(id);
        }
        DeviceInfo update = MapstructUtils.convert(bo, DeviceInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DeviceInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除手环-设备信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public String selectDeviceNumberById(Long id) {

        return baseMapper.selectVoById(id) != null ? baseMapper.selectVoById(id).getDeviceNumber(): "";
    }

    @Override
    public String selectNameById(Long id) {
         return baseMapper.selectVoById(id) != null ? baseMapper.selectVoById(id).getName() : "";
    }

    @Override
    public Long selectIdByName(String name) {
        DeviceInfoVo deviceInfoVo = baseMapper.selectVoOne(new LambdaQueryWrapper<DeviceInfo>()
            .select(DeviceInfo::getId)
            .eq(DeviceInfo::getUserName, name));
        Long id = null;
        if (deviceInfoVo != null) {
            id = deviceInfoVo.getId();
        }
        return id;
    }


}
